//
//  AboutUsViewController.swift
//  SwiftStudy
//
//  Created by chang on 2018/3/22.
//  Copyright © 2018年 chang. All rights reserved.
//

import UIKit

class AboutUsViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        self.view.backgroundColor = UIColor.defaultBackgroundColor
        self.navigationItem.title = "关于我们"
        
        let watermark = self.getWatermark(originalView: self.view, aTitle: "热烈欢迎您", markFont: UIFont.systemFont(ofSize: 26), markColor: nil)
        let bgImg = UIImageView.init(image: watermark)
        bgImg.frame = CGRect.init(x: 0, y: 0, width: MAINSCREEN_WIDTH, height: MAINSCREEN_HEIGHT)
        self.view.addSubview(bgImg)
        
        let titleLabel = UILabel.init(frame: CGRect.init(x: 30, y: 100, width: MAINSCREEN_WIDTH-60, height: 100))
        titleLabel.textAlignment = .center
        titleLabel.textColor = UIColor.defaultYellowColor
        titleLabel.font = UIFont.boldSystemFont(ofSize: 17)
        titleLabel.textAlignment = .center
        titleLabel.numberOfLines = 0
        titleLabel.text = "欢迎你的到来，如发现有问题欢迎指出，谢谢🙏大家"
        self.view.addSubview(titleLabel)
        
        let imgStr = String.init(format: "xxy"+"%03d", Int(arc4random()%40)+1)
        let imageView = UIImageView.init(frame: CGRect.init(x: 50, y: titleLabel.bottom+30, width: MAINSCREEN_WIDTH-50*2, height: MAINSCREEN_WIDTH-50*2))
        imageView.image = UIImage.init(named: imgStr)
        self.view.addSubview(imageView)
    }
    
    
    
    /**
     根据目标图片制作一个盖水印的图片
     
     @param originalView 添加的源View
     @param aTitle 水印文字
     @param markFont 水印文字font(如果不传默认为23)
     @param markColor 水印文字颜色(如果不传递默lightGray)
     @return 返回盖水印的图片
     */
    let HORIZONTAL_SPACE = CGFloat(40)  //水平间距
    let VERTICAL_SPACE = CGFloat(40)    //竖直间距
    let CG_TRANSFORM_ROTATION = CGFloat((Double.pi/2)/3) //旋转角度(正旋45度 || 反旋45度)
    func getWatermark(originalView: UIView, aTitle: NSString, markFont: UIFont?, markColor: UIColor?) -> UIImage {
        
        //如果没有设置 就取默认
        let aFont = markFont ?? UIFont.systemFont(ofSize: 23)
        let aColor = markColor ?? UIColor.lightGray
        
        //原始view的宽高
        let viewWidth = originalView.height
        let viewHeight = originalView.height
        
        //为了防止图片失真，绘制区域宽高和原始图片宽高一样
        UIGraphicsBeginImageContext(CGSize.init(width: viewWidth, height: viewHeight))
        //先将原始view绘制上
        originalView.draw(CGRect.init(x: 0, y: 0, width: viewHeight, height: viewHeight))
        
        //sqrtLength：原始view的对角线length。在水印旋转矩阵中只要矩阵的宽高是原始view的对角线长度，无论旋转多少度都会覆盖住水印，不会有空白。
        let sqrtLength = CGFloat( sqrtf(Float(viewWidth*viewWidth + viewHeight*viewHeight)))
        
        //文字的属性
        let attrDic = [
            NSAttributedStringKey.font : aFont,  //设置字体大小
            NSAttributedStringKey.foregroundColor : aColor  //设置文字颜色
        ]
        let attrStr = NSMutableAttributedString.init(string: aTitle as String, attributes: attrDic) as NSMutableAttributedString
        
        //绘制文字的宽高
        let strWidth = attrStr.size().width
        let strHeight = attrStr.size().height
        
        //开始旋转上下文矩阵，绘制水印文字
        let context = UIGraphicsGetCurrentContext()
        //将绘制原点（0，0）调整到 源view 的中心
        context?.concatenate(CGAffineTransform.init(translationX: viewWidth/2, y: viewHeight/2))
        //以绘制原点为中心旋转
        context?.concatenate(CGAffineTransform.init(rotationAngle: CG_TRANSFORM_ROTATION))
        
        //将绘制原点恢复初始值，保证当前context中心和源view的中心处在一个点(当前context已经旋转，所以绘制出的任何layer都是倾斜的)
        context?.concatenate(CGAffineTransform.init(translationX: -viewWidth/2, y: -viewHeight/2))
        //计算需要绘制的列数和行数
        let horCount = sqrtLength / (strWidth + HORIZONTAL_SPACE) + 1
        let verCount = sqrtLength / (strHeight + VERTICAL_SPACE) + 1
        //此处计算出需要绘制水印文字的起始点，由于水印区域要大于图片区域所以起点在原有基础上移
        let orignX = -(sqrtLength-viewWidth)/2
        let orignY = -(sqrtLength-viewHeight)/2
        //在每列绘制时X坐标叠加
        var tempOrignX = orignX
        //在每行绘制时Y坐标叠加
        var tempOrignY = orignY
        
        for i in Int(0)...Int(horCount*verCount-1)
        {
            aTitle.draw(in: CGRect.init(x: tempOrignX, y: tempOrignY, width: strWidth, height: strHeight), withAttributes: attrDic)
            
            if (i%Int(horCount) == 0 && i != 0) {
                tempOrignX = orignX
                tempOrignY += (strHeight + VERTICAL_SPACE)
            }else{
                tempOrignX += (strWidth + HORIZONTAL_SPACE);
            }
        }
        
        //根据上下文制作成图片
        let finalImg = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext();
        //CGContext.restoreGState(context)
        return finalImg!
    }
    
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

}


